Linking Objects Through Properties, Methods, and Events

Objectives


Discussion

Object-Oriented Programming

Meaning

Example for textbox

Properties Characteristic of an object, like an adjective Text
Methods Something an object does, like a verb Hide()
Events Something an object responds to MouseLeave

Private Sub txtNewText_MouseLeave _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles txtNewText.MouseLeave
     btnChange.Text = txtNewText.Text
     txtNewText.Hide()
End Sub

Available Events for an Object

Getting Help

VS Features

Back to top


Demonstration

This demonstration will show you how you will use properties, methods, and events to write programs so that different objects will interact with each other.  Understanding this demonstration is critical!

1.  Create a new project.  Make sure that you give it an appropriate name such as Lesson1_3Demo.

2.  Add a button to a form.  Change its Name property to "btnChange" and its text property to "Change Me".  The Name property is what you will use in code to refer to the control.  It is good programming practice to give each control a relevant name so that your code is easy to read.

3.  Add a textbox to the form.  A textbox is a control for getting and presenting text.  Change its name property to "txtNewText".  Run your program. 

4. Before we continue with our program we will check out Help .  VS accesses an extensive help library.  If you click on Help in the main menu you will see several options for getting help. For example you can use Help.Contents, Help.Index,  or Help.Search to get help. 

5. Click Help...Dynamic Help and a Dynamic Help window will appear. The term "Dynamic" means that the window will change depending on what you are doing.   Now click on the button on your form and observe the Dynamic Help window.  Click on the textbox on your form and observe the Dynamic Help window.  Notice that the topics change according to what you are working on.

6.  One of the greatest wonders of VS is the "F1" key.  We will demonstrate.  Click on the textbox on your form.  Hit the "F1" key.  What happens?  Hopefully a new window opens with help on the textbox.  If you are typing code in the window and hit "F1" you will get information on what you are typing.  Try it.  It is amazing.  Remember "F1".  It is your friend.

7.  Let's explore Help.  It is very important to take a few minutes to check out the information that is available in Help. As a programmer you will often need to refer to Help.  There is way too much to learn about VB and it is nearly impossible to cover every topic in any course anywhere.  So you will need to use Help and other resources to learn how to use features that you may need for your programs.  Click on the textbox and hit F1.  Scroll down through the window. Notice that the properties, methods, and events are listed.  Skim through the properties. Which property can be used to make the textbox hold more than one line of text?  What does the clear method do? 

8.  Notice at the end of each lesson is a section "Links & Help".  This section contains references to useful websites and Help articles. You should check out these links.   You should also become comfortable with searching for help on your own.

9.  The textbox has a number of properties.  One of these properties is the text property.  The text property contains what is present in the textbox.  You can change the value of any property using the Properties Window; you have already done this.  You can also change the value of a property in code and you can use the property in code.  Let's experiment with the controls txtNewText and btnChange.  Double click btnChange so that the code for the click event appears.  Type the following highlighted code in this event handler:

Private Sub btnChange_Click(ByVal sender As System... 
     btnChange.Text = txtNewText.Text
End Sub

10.  Run your program.  Experiment.  What happens?  Notice that we are making the text on the button equal the text in the textbox.  It is critical that you understand that you can assign values to a property and use values of properties.

11.  Now we will use a method to demonstrate how they are like verbs and do things.  Type the following highlighted code in the event handler for the click event for the button:

Private Sub btnChange_Click(ByVal sender As System... 
     btnChange.Text = txtNewText.Text
     txtNewText.clear()
End Sub

12.  Run your program and experiment.

13.  We'll use one more method.   Add the following highlighted code:

Private Sub btnChange_Click(ByVal sender As System... 
     btnChange.Text = txtNewText.Text
     txtNewText.clear()
     txtNewText.focus()
End Sub

14. Run your program and experiment.  What happens to the cursor after you click the button?  Now go to the code window, click on the word "focus()" and check out what Help tells you.

15.  Now we'll check out some of VS's features that make it a friendly environment for programming.  Go into the code window and click somewhere inside the click event for the button.  Make sure that you are on a clear line with no other code on it.  Type in the following:  "txtnewtext."  Make sure you type in the period.  Notice what happens.  VS prompts you with the properties and methods for a textbox.  Now type a "t" after the period.  Notice what happens.  Now type "extl" after the period.  Notice that textLength is highlighted.  Now hit the spacebar and VS will finish the word for you.  You could also hit the return button.  This feature of VS is incredibly powerful.  If you are using a control and are not sure what to do with it, you can type in the name of the control and scroll through the properties and methods until you find something that will help you.

Back to top

Exercises

1.  When you click on a text box, the length of the text in the box should be displayed in a messagebox.

2.  When you move the cursor over a textbox, the words "you entered" should appear in the textbox.  When you move the mouse out of the textbox, the words "You left" should appear in the textbox. 

3.  Use the .paste method of a textbox to paste text from the clipboard into a textbox when you click a button.

4.  When the user changes the text in a textbox, the text of the button should be set equal to the text in the textbox.

5. When the user clicks a button, move it so its top is set to 200 and its left is set to 100.

6. When the user clicks a textbox, set its top to the value in the textbox.  Ensure that you put a number in the textbox in the properties window or you will get an error when you click on the textbox.

7.  When the user clicks a button, close the form using the close method.  Remember that you can refer to the form using "Me".

8. When the user clicks a button, move it 10 to the right.

9. When the user resizes a form (by dragging its corner) display the width of the form in a label.

10. When the user clicks a monthly calendar, display the selected date in a messagebox.

11. When the user clicks a button, clear the text in a textbox.  You can do this using a method or setting a property to "".

Back to top
Links & Help
Back to top